package bb.lanxing.lib.widget;

import android.util.SparseIntArray;
import android.view.ViewGroup;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.RecyclerView.ViewHolder;
import bb.lanxing.lib.widget.PinnedHeaderRecyclerView;

public abstract class SectionedRecyclerViewAdapter<H extends RecyclerView.ViewHolder, VH extends RecyclerView.ViewHolder,
        F extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<RecyclerView.ViewHolder>
        implements PinnedHeaderRecyclerView.PinnedSectionedHeaderAdapter {
    private static final int ITEM_POSITION_MASK = 0xFFFF;
    private static final int POSITION_TYPE_HEADER = 1;
    private static final int POSITION_TYPE_FOOTER = 2;
    private static final int POSITION_TYPE_ITEM = 4;
    private static final int POSITION_TYPE_MASK = 15;
    private static final int POSITION_TYPE_SHIFT = 28;
    private static final int SECTION_INDEX_SHIFT = 16;
    private static final int SECTION_INDEX_MASK = 0x0FFF;
    protected static final int TYPE_ITEM = -3;
    protected static final int TYPE_SECTION_FOOTER = -2;
    protected static final int TYPE_SECTION_HEADER = -1;
    private SparseIntArray mPackedPositions = null;

    private static int getPositionForItem(int i) {
        return i & ITEM_POSITION_MASK;
    }

    private static int getSectionIndex(int i) {
        return (i >>> SECTION_INDEX_SHIFT) & SECTION_INDEX_MASK;
    }

    private static boolean isItem(int i) {
        return ((i >>> POSITION_TYPE_SHIFT) & POSITION_TYPE_MASK) == POSITION_TYPE_ITEM;
    }

    private static boolean isSectionFooter(int i) {
        return ((i >>> POSITION_TYPE_SHIFT) & POSITION_TYPE_MASK) == POSITION_TYPE_FOOTER;
    }

    private static boolean isSectionHeader(int i) {
        return ((i >>> POSITION_TYPE_SHIFT) & POSITION_TYPE_MASK) == POSITION_TYPE_HEADER;
    }

    protected abstract int getItemCountForSection(int i);

    public abstract int getSectionCount();

    protected int getSectionFooterViewType(int i) {
        return TYPE_SECTION_FOOTER;
    }

    public int getSectionHeaderViewType(int i) {
        return TYPE_SECTION_HEADER;
    }

    protected int getSectionItemViewType(int i, int i2) {
        return TYPE_ITEM;
    }

    public abstract CharSequence getSectionTitle(int i);

    protected abstract boolean hasFooterInSection(int i);

    public boolean isSectionFooterViewType(int i) {
        return i == TYPE_SECTION_FOOTER;
    }

    public boolean isSectionHeaderViewType(int i) {
        return i == TYPE_SECTION_HEADER;
    }

    protected abstract void onBindItemViewHolder(VH vh, int i, int i2);

    protected abstract void onBindSectionFooterViewHolder(F f, int i);

    protected abstract void onBindSectionHeaderViewHolder(H h, int i);

    protected abstract VH onCreateItemViewHolder(ViewGroup viewGroup, int i);

    protected abstract F onCreateSectionFooterViewHolder(ViewGroup viewGroup, int i);

    protected abstract H onCreateSectionHeaderViewHolder(ViewGroup viewGroup, int i);

    public SectionedRecyclerViewAdapter() {
        registerAdapterDataObserver(new SectionDataObserver());
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        setupIndices();
    }

    @Override
    public int getItemCount() {
        SparseIntArray sparseIntArray = this.mPackedPositions;
        if (sparseIntArray != null) {
            return sparseIntArray.size();
        }
        return 0;
    }

    public void setupIndices() {
        if (this.mPackedPositions == null) {
            this.mPackedPositions = new SparseIntArray();
        }
        this.mPackedPositions.clear();
        int sectionCount = getSectionCount();
        int i = 0;
        for (int i2 = 0; i2 < sectionCount; i2++) {
            int i3 = i2 << SECTION_INDEX_SHIFT;
            int i4 = i + 1;
            this.mPackedPositions.put(i, 0x10000000 | i3);
            int itemCountForSection = getItemCountForSection(i2);
            int i5 = 0;
            while (i5 < itemCountForSection) {
                this.mPackedPositions.put(i4, 0x40000000 | i3 | i5);
                i5++;
                i4++;
            }
            if (hasFooterInSection(i2)) {
                this.mPackedPositions.put(i4, 0x20000000 | i3);
                i = i4 + 1;
            } else {
                i = i4;
            }
        }
    }

    private int getPackedPosition(int i) {
        SparseIntArray sparseIntArray = this.mPackedPositions;
        if (sparseIntArray != null) {
            return sparseIntArray.get(i);
        }
        return 0;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        if (isSectionHeaderViewType(i)) {
            return onCreateSectionHeaderViewHolder(viewGroup, i);
        }
        if (isSectionFooterViewType(i)) {
            return onCreateSectionFooterViewHolder(viewGroup, i);
        }
        return onCreateItemViewHolder(viewGroup, i);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i) {
        int packedPosition = getPackedPosition(i);
        int sectionIndex = getSectionIndex(packedPosition);
        int positionForItem = getPositionForItem(packedPosition);
        if (isSectionHeaderPosition(i)) {
            onBindSectionHeaderViewHolder((H) viewHolder, sectionIndex);
        } else if (isSectionFooterPosition(i)) {
            onBindSectionFooterViewHolder((F) viewHolder, sectionIndex);
        } else {
            onBindItemViewHolder((VH) viewHolder, sectionIndex, positionForItem);
        }
    }

    @Override
    public int getItemViewType(int i) {
        if (this.mPackedPositions == null) {
            setupIndices();
        }
        int packedPosition = getPackedPosition(i);
        int sectionIndex = getSectionIndex(packedPosition);
        int positionForItem = getPositionForItem(packedPosition);
        if (isSectionHeaderPosition(i)) {
            return getSectionHeaderViewType(sectionIndex);
        }
        if (isSectionFooterPosition(i)) {
            return getSectionFooterViewType(sectionIndex);
        }
        return getSectionItemViewType(sectionIndex, positionForItem);
    }

    @Override
    public boolean isSectionHeaderPosition(int i) {
        if (this.mPackedPositions == null) {
            setupIndices();
        }
        return isSectionHeader(getPackedPosition(i));
    }

    public boolean isSectionFooterPosition(int i) {
        if (this.mPackedPositions == null) {
            setupIndices();
        }
        return isSectionFooter(getPackedPosition(i));
    }

    private class SectionDataObserver extends RecyclerView.AdapterDataObserver {
        private SectionDataObserver() {
        }

        @Override
        public void onChanged() {
            SectionedRecyclerViewAdapter.this.setupIndices();
        }

        @Override
        public void onItemRangeChanged(int i, int i2) {
            SectionedRecyclerViewAdapter.this.setupIndices();
        }

        @Override
        public void onItemRangeInserted(int i, int i2) {
            SectionedRecyclerViewAdapter.this.setupIndices();
        }

        @Override
        public void onItemRangeMoved(int i, int i2, int i3) {
            SectionedRecyclerViewAdapter.this.setupIndices();
        }

        @Override
        public void onItemRangeRemoved(int i, int i2) {
            SectionedRecyclerViewAdapter.this.setupIndices();
        }
    }

    public int getItemPosition(int i) {
        for (int i2 = 0; i2 < getItemCount(); i2++) {
            int i3 = this.mPackedPositions.get(i2);
            if (isSectionHeader(i3) && getSectionIndex(i3) == i) {
                return i2;
            }
        }
        return -1;
    }

    @Override
    public int getSectionForPosition(int i) {
        return getSectionIndex(this.mPackedPositions.get(i));
    }

    public int getPositionInSection(int i) {
        return getPositionForItem(this.mPackedPositions.get(i));
    }
}
